package cdu.sl.aichatdemo.utils;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * 构建缓存工具类
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class CacheUtil {

    private final StringRedisTemplate stringRedisTemplate;


    public Boolean saveToCacheConsistence(String key, String value) {
        stringRedisTemplate.opsForValue().set(key, value);
        return true;
    }


    public Boolean saveToCacheWithExpireTime(String key, String value, Integer timeout, TimeUnit unit) {
        stringRedisTemplate.opsForValue().set(key, value, timeout, unit);
        return true;
    }


    public Boolean cleanFromCache(String key) {
        return stringRedisTemplate.delete(key);
    }


    public String getFormCache(String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }


}
